package evolution.problem;

import static java.lang.Math.PI;
import static java.lang.Math.cos;

public class RastriginMinimization extends AbstractMinimizationProblem {

	private static final int A = 10;
	public static final ProblemConstraints CONSTRAINTS = new ProblemConstraints(
			2, -5.12, 5.12);

	public RastriginMinimization() {
		super(CONSTRAINTS);
	}

	@Override
	public double getValue(double[] x) {
		int length = x.length;

		double value = A * length;
		for (int i = 0; i < length; i++) {
			double x_i = x[i];
			value += x_i * x_i - A * cos(2 * PI * x_i);
		}

		return value;
	}

	@Override
	public String getGnuplotFunction() {
		return "10*2 + x**2-10*cos(2*pi*x) + y**2-10*cos(2*pi*y)";
	}

	@Override
	public String getGnuplotFunctionName() {
		return "rastrigin";
	}
}
